package com.musiklegal;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class MerchantParser extends DefaultHandler 
{
	
	MerchantEntity aMerchant;
	boolean mName=false,mKey=false,mImg=false;
	StringBuffer buffer;
	
	MerchantParser()
	{
		buffer=new StringBuffer();
		
	}
	
	
	
	public void reset()
	{
		buffer = new StringBuffer();
	}
	
	public void startDocument() throws SAXException
	{
	}
	
	public void endDocument() throws SAXException
	{
		System.gc();	
	}

	public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
	{
		if(localName.compareTo("merchant")==0)
		{
			aMerchant = new MerchantEntity(atts.getValue("id"));
		}
		else if(localName.compareTo("catalogue_name")==0)
			mName=true;
		else if(localName.compareTo("merchant_key")==0)
			mKey=true;
		else if(localName.compareTo("company_logo")==0)
			mImg=true;
		
		
	}
	
	
	public void endElement(String namespaceURI, String localName, String qName)
	throws SAXException 
	{
		if(localName.compareTo("merchant")==0)
			{
				OnlineC.merch.add(aMerchant);
			}
		else if(localName.compareTo("catalogue_name")==0)
			{
				aMerchant.name=(buffer.toString());
				buffer = new StringBuffer();
				mName=false;
			}
		else if(localName.compareTo("merchant_key")==0)
			{
			aMerchant.key=(buffer.toString());
			buffer = new StringBuffer();
				mKey=false;
			}
		else if(localName.compareTo("company_logo")==0)
			{
			aMerchant.imgUrl=(buffer.toString());
			buffer = new StringBuffer();
				mImg=false;
			}
	}
	
	 public void characters(char ch[], int start, int length) 
	 {
		 if(mName)
		 {
			 buffer.append(new String(ch,start,length));
		 }
		 else if(mKey)
		 {
			 buffer.append(new String(ch,start,length));
		 }
		 else if(mImg)
		 {
			 buffer.append(new String(ch,start,length));
		 }
		 else if(mName)
		 {
			 buffer.append(new String(ch,start,length));
		 }
	 }
}
